﻿using CATLTempUpdate.CommonLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Speech.Synthesis;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace CATLTempUpdate
{
    public partial class FormTemperatureStocking : Form
    {
        [DllImport("user32.dll")]//*********************拖动无窗体的控件 

        public static extern bool ReleaseCapture();

        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        public const int WM_SYSCOMMAND = 0x0112;

        public const int SC_MOVE = 0xF010;

        public const int HTCAPTION = 0x0002;
        int RowNumber = 0, Columns = 0, LocationY = 0, labSenseWidth = 30;
        Label labSense;
        int PowerColumnNo = 5;
        int SenseCountNo=20;
        int columnNo = 5;
        Thread OffLineCheckThread;      //心跳检测线程
        Thread WriteLogThread;          //Log写入线程
        ToolTip tip;
        public static int ShowSenseCodeId;
        int serialMaxRowNo = 4;
        string codeTitle = "";
        double avgpanel = 1.0;
        //PLC IP及端口定义
        int port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["FormationPort1"]);
        static SocketTest st1 = new SocketTest();
        static SocketTest st2 = new SocketTest();
        static SocketTest st3 = new SocketTest();
        static SocketTest st4 = new SocketTest();
        static SocketTest st5 = new SocketTest();
        TcpReciveAndSend Readyte;   //实例化服务端
        bool WantExit = false;
        //bool StartLog = true;
        string ip1 = System.Configuration.ConfigurationManager.AppSettings["FormationAddress1"];
        string ip2 = System.Configuration.ConfigurationManager.AppSettings["FormationAddress2"];
        string ip3 = System.Configuration.ConfigurationManager.AppSettings["FormationAddress3"];
        string ip4 = System.Configuration.ConfigurationManager.AppSettings["FormationAddress4"];
        string ip5 = System.Configuration.ConfigurationManager.AppSettings["FormationAddress5"];
        public static object ChangeTimeLock = new object();
        //列架台温度写入Thread
        public static bool PLCConnectStatus = true;        //PLC连接状态
        public static string ConnectFalseInfo = string.Empty;        //PLC连接失败架台


        Thread Stage1DataWriteThread;
        Thread Stage2DataWriteThread;
        Thread Stage3DataWriteThread;
        Thread Stage4DataWriteThread;
        Thread Stage5DataWriteThread;
        Thread ShowErrorLogThread;

        public List<string> iplist = new List<string>();
        //public List<string> portlist = new List<string>();
        public List<SocketTest> socketlist = new List<SocketTest>();
        public List<string> Stage1PLCAddress = new List<string>();
        public List<string> Stage2PLCAddress = new List<string>();
        public List<string> Stage3PLCAddress = new List<string>();
        public List<string> Stage4PLCAddress = new List<string>();
        public List<List<string>> StagePlcAddressList = new List<List<string>>();

        bool initialflag = false;



        public FormTemperatureStocking()
        {
            InitializeComponent();
            this.MaximizeBox = false;
        }

        private void FormTemperatureStocking_Load(object sender, EventArgs e)
        {
            BindSerialOne();
            InitTempDataDT();
            InitAliveTraceDT();
            OffLineCheckThread = new Thread(CheckSenseOffLine);
            OffLineCheckThread.Start();
            WriteLogThread = new Thread(WriteLog);
            WriteLogThread.Start();

            InitServer();

            InitialPLC();
            Stage1DataWriteThread = new Thread(new ParameterizedThreadStart(UpdateStageTempDataToPLC));
            Stage1DataWriteThread.Start(1);
            Stage2DataWriteThread = new Thread(new ParameterizedThreadStart(UpdateStageTempDataToPLC));
            Stage2DataWriteThread.Start(2);
            Stage3DataWriteThread = new Thread(new ParameterizedThreadStart(UpdateStageTempDataToPLC));
            Stage3DataWriteThread.Start(3);
            Stage4DataWriteThread = new Thread(new ParameterizedThreadStart(UpdateStageTempDataToPLC));
            Stage4DataWriteThread.Start(4);
            Stage5DataWriteThread = new Thread(new ParameterizedThreadStart(UpdateStageTempDataToPLC));
            Stage5DataWriteThread.Start(5);

        }

        private void labelExit_Click(object sender, EventArgs e)
        {
            try
            {
                OffLineCheckThread.Abort();
                WriteLogThread.Abort();
                TcpReciveAndSend.exit = true;
                WantExit = true;
                this.Close();
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ColumnRowChNo"></param>
        public void ShowErrorLog(Object  ColumnRowChNo)
        {
            string[] columnrowchno= ColumnRowChNo.ToString().Split(',');
            if (columnrowchno.Length >= 3)
            {
                string warningmsg = "架台:" + columnrowchno[0] + "列,层:" + columnrowchno[1] + ",通道：" + columnrowchno[2] + "温度探针短路，请及时查看探针接线是否异常";
                displayInfo(warningmsg,true);
                Thread.Sleep(5000);
            }
        }
        
        /// <summary>
        /// 绑定1系的架台
        /// SZW 2017-10-13 10:49:55
        /// </summary>
        private void BindSerialOne()
        {
            RowNumber = 0; LocationY = 0; Columns = 1;

            for (int m = 1; m <= 4; m++)
            {
                for (int i = 0; i < PowerColumnNo; i++)
                {
                    pubFun(m, pnlGuideOne, m, i);// 绑定排数据公用方法
                }
            }
            
            
        }

        public void InitServer()
        {
            
            for (int j=0;j<5;j++)
            {
                List<List<int>> RowData = new List<List<int>>();
                for (int m = 0; m < 4; m++)
                {
                    //初始化24 Channel Data
                    List<int> ChnoData = new List<int>();
                    for (int i = 0; i < 24; i++)
                    {
                        ChnoData.Add(100);
                    }
                    RowData.Add(ChnoData);
                }
                SenseDataCollection.TempDataList.Add(RowData);
            }
           
            
            Readyte = new TcpReciveAndSend();
        }

        private void FormTemperatureStocking_SizeChanged(object sender, EventArgs e)
        {

            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();   //隐藏窗体
                notifyIcon1.Visible = true; //使托盘图标可见
            }
        }
        private void labelTaskManage_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.WindowState = FormWindowState.Minimized;//最小化 
        }

        /// <summary>
        /// 写日志
        /// </summary>
        public void WriteLog()
        {
            while (!WantExit)
            {
                while (Log.Trace.Count > 0)
                {
                    int DataLength = Log.Trace.Count;
                    Log.WriteLog(LogFile.Trace, Log.Trace, DataLength);
                    if (Log.Trace.Count > 0)
                    {
                        Log.Trace.RemoveRange(0, DataLength);
                    }
                }

                while (Log.MeasureError.Count > 0)
                {
                    int DataLength = Log.MeasureError.Count;
                    Log.WriteLog(LogFile.MeasureError, Log.MeasureError, DataLength);
                    if (Log.MeasureError.Count > 0)
                    {
                        Log.MeasureError.RemoveRange(0, DataLength);
                    }
                }
                while (Log.SQL.Count > 0)
                {
                    int DataLength = Log.SQL.Count;
                    Log.WriteLog(LogFile.SQL, Log.SQL, DataLength);
                    if (Log.SQL.Count > 0)
                    {
                        Log.SQL.RemoveRange(0, DataLength);
                    }
                }
                while (Log.SystemError.Count > 0)
                {
                    int DataLength = Log.SystemError.Count;
                    Log.WriteLog(LogFile.SystemError, Log.SystemError, DataLength);
                    if (Log.SystemError.Count > 0)
                    {
                        Log.SystemError.RemoveRange(0, DataLength);
                    }
                }
                Thread.Sleep(10);
            }
        }
        /// <summary>
        /// 批量将架台N温度数据写到plc
        /// </summary>
        /// <param name="ColumnNo">列NO</param>
        public void UpdateStageTempDataToPLC(Object ColumnNoObj)
        {
            int ColumnNo = (int)ColumnNoObj;
            while (!WantExit)
            {
                try
                {
                    //遍历层
                    for (int n = 0; n < SenseDataCollection.TempDataList[ColumnNo - 1].Count; n++)
                    {
                        string CellTemp = "";
                        //遍历24个通道的数据
                        bool IfFreshTempData = true;
                        if (SenseDataCollection.TempDataList[ColumnNo - 1][n][0] == 100)
                        {
                            IfFreshTempData = false;
                            continue;
                        }
                        else
                        {
                            for (int i = 0; i < SenseDataCollection.TempDataList[ColumnNo - 1][n].Count; i++)
                            {
                                if (SenseDataCollection.TempDataList[ColumnNo - 1][n][i] > 60000)
                                {
                                    if (ShowErrorLogThread == null || !ShowErrorLogThread.IsAlive)
                                    {
                                        ShowErrorLogThread = new Thread(new ParameterizedThreadStart(ShowErrorLog));
                                        ShowErrorLogThread.Start((ColumnNo) + "," + (n + 1) + "," + (i + 1));
                                    }

                                    CellTemp += Convert.ToInt32("111").ToString("X").PadLeft(4, '0');
                                }
                                else
                                {
                                    CellTemp += SenseDataCollection.TempDataList[ColumnNo - 1][n][i].ToString("X").PadLeft(4, '0');
                                }
                                
                            }
                        }
                        if(IfFreshTempData)
                        {
                            socketlist[ColumnNo - 1].WriteByteMultipleStatusValue(StagePlcAddressList[n][0], 24, CellTemp);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.AddLog(LogFile.SystemError,"架台"+ColumnNo+"数据写入PLC失败！"+ex.ToString());
                }
                Thread.Sleep(400);
            }
            
        }

        public void InitialPLC()
        {


            iplist.Add(ip1);
            iplist.Add(ip2);
            iplist.Add(ip3);
            iplist.Add(ip4);
            iplist.Add(ip5);


            //将不同架台的Socket放入list
            socketlist.Add(st1);
            socketlist.Add(st2);
            socketlist.Add(st3);
            socketlist.Add(st4);
            socketlist.Add(st5);

            bool ConnectFlag = true;
            try
            {
                if (!st1.CreateClient(ip1, port))
                {
                    Log.AddLog(LogFile.SystemError, "初始化PLC连接" + ip1 + ":" + port + "失败");
                    MessageBox.Show("初始化PLC连接" + ip1 + ":" + port + "失败", "警告", MessageBoxButtons.OK);
                    Thread.Sleep(100);
                    ConnectFlag = false;
                }

                if (!st2.CreateClient(ip2, port))
                {
                    Log.AddLog(LogFile.SystemError, "初始化PLC连接" + ip2 + ":" + port + "失败");
                    MessageBox.Show("初始化PLC连接" + ip2 + ":" + port + "失败", "警告", MessageBoxButtons.OK);
                    Thread.Sleep(100);
                    ConnectFlag = false;
                }
                if (!st3.CreateClient(ip3, port))
                {
                    Log.AddLog(LogFile.SystemError, "初始化PLC连接" + ip3 + ":" + port + "失败");
                    MessageBox.Show("初始化PLC连接" + ip3 + ":" + port + "失败", "警告", MessageBoxButtons.OK);
                    Thread.Sleep(100);
                    ConnectFlag = false;
                }


                if (!st4.CreateClient(ip4, port))
                {
                    Log.AddLog(LogFile.SystemError, "初始化PLC连接" + ip4 + ":" + port + "失败");
                    MessageBox.Show("初始化PLC连接" + ip4 + ":" + port + "失败", "警告", MessageBoxButtons.OK);
                    Thread.Sleep(100);
                    ConnectFlag = false;
                }

                if (!st5.CreateClient(ip5, port))
                {
                    Log.AddLog(LogFile.SystemError, "初始化PLC连接" + ip5 + ":" + port + "失败");
                    MessageBox.Show("初始化PLC连接" + ip5 + ":" + port + "失败", "警告", MessageBoxButtons.OK);
                    Thread.Sleep(100);
                    ConnectFlag = false;
                }
                
            }
            catch (Exception ex)
            {
                Log.AddLog(LogFile.SystemError, "与PLC建立连接失败" + ex.ToString());
            }


            //将PLCAddress放入对应的架台层
            //1层温度地址
            //CHNO  1-9
            Stage1PLCAddress.Add("130");
            Stage1PLCAddress.Add("131");
            Stage1PLCAddress.Add("132");
            Stage1PLCAddress.Add("133");
            Stage1PLCAddress.Add("134");
            Stage1PLCAddress.Add("135");
            Stage1PLCAddress.Add("136");
            Stage1PLCAddress.Add("137");
            Stage1PLCAddress.Add("138");

            //temp1chno0-18
            Stage1PLCAddress.Add("139");
            Stage1PLCAddress.Add("13A");
            Stage1PLCAddress.Add("13B");
            Stage1PLCAddress.Add("13C");
            Stage1PLCAddress.Add("13D");
            Stage1PLCAddress.Add("13E");
            Stage1PLCAddress.Add("13F");
            Stage1PLCAddress.Add("140");
            Stage1PLCAddress.Add("141");
            //环境温度1-3
            Stage1PLCAddress.Add("142");
            Stage1PLCAddress.Add("143");
            Stage1PLCAddress.Add("144");
            //环境温度4-6
            Stage1PLCAddress.Add("145");
            Stage1PLCAddress.Add("146");
            Stage1PLCAddress.Add("147");

            //2层温度地址
            //CHNO  1-9
            Stage2PLCAddress.Add("160");
            Stage2PLCAddress.Add("161");
            Stage2PLCAddress.Add("162");
            Stage2PLCAddress.Add("163");
            Stage2PLCAddress.Add("164");
            Stage2PLCAddress.Add("165");
            Stage2PLCAddress.Add("166");
            Stage2PLCAddress.Add("167");
            Stage2PLCAddress.Add("168");

            //temp1chno0-18
            Stage2PLCAddress.Add("169");
            Stage2PLCAddress.Add("16A");
            Stage2PLCAddress.Add("16B");
            Stage2PLCAddress.Add("16C");
            Stage2PLCAddress.Add("16D");
            Stage2PLCAddress.Add("16E");
            Stage2PLCAddress.Add("16F");
            Stage2PLCAddress.Add("170");
            Stage2PLCAddress.Add("171");
            //环境温度1-3
            Stage2PLCAddress.Add("172");
            Stage2PLCAddress.Add("173");
            Stage2PLCAddress.Add("174");
            //环境温度4-6
            Stage2PLCAddress.Add("175");
            Stage2PLCAddress.Add("176");
            Stage2PLCAddress.Add("177");

            //3层温度地址
            //CHNO  1-9
            Stage3PLCAddress.Add("190");
            Stage3PLCAddress.Add("191");
            Stage3PLCAddress.Add("192");
            Stage3PLCAddress.Add("193");
            Stage3PLCAddress.Add("194");
            Stage3PLCAddress.Add("195");
            Stage3PLCAddress.Add("196");
            Stage3PLCAddress.Add("197");
            Stage3PLCAddress.Add("198");

            //temp1chno0-18
            Stage3PLCAddress.Add("199");
            Stage3PLCAddress.Add("19A");
            Stage3PLCAddress.Add("19B");
            Stage3PLCAddress.Add("19C");
            Stage3PLCAddress.Add("19D");
            Stage3PLCAddress.Add("19E");
            Stage3PLCAddress.Add("19F");
            Stage3PLCAddress.Add("1A0");
            Stage3PLCAddress.Add("1A1");
            //环境温度1-3
            Stage3PLCAddress.Add("1A2");
            Stage3PLCAddress.Add("1A3");
            Stage3PLCAddress.Add("1A4");
            //环境温度4-6
            Stage3PLCAddress.Add("1A5");
            Stage3PLCAddress.Add("1A6");
            Stage3PLCAddress.Add("1A7");


            //4层温度地址
            //CHNO  1-9
            Stage4PLCAddress.Add("1C0");
            Stage4PLCAddress.Add("1C1");
            Stage4PLCAddress.Add("1C2");
            Stage4PLCAddress.Add("1C3");
            Stage4PLCAddress.Add("1C4");
            Stage4PLCAddress.Add("1C5");
            Stage4PLCAddress.Add("1C6");
            Stage4PLCAddress.Add("1C7");
            Stage4PLCAddress.Add("1C8");
            //temp1chno0-18
            Stage4PLCAddress.Add("1C9");
            Stage4PLCAddress.Add("1CA");
            Stage4PLCAddress.Add("1CB");
            Stage4PLCAddress.Add("1CC");
            Stage4PLCAddress.Add("1CD");
            Stage4PLCAddress.Add("1CE");
            Stage4PLCAddress.Add("1CF");
            Stage4PLCAddress.Add("1D0");
            Stage4PLCAddress.Add("1D1");
            //环境温度1-3
            Stage4PLCAddress.Add("1D2");
            Stage4PLCAddress.Add("1D3");
            Stage4PLCAddress.Add("1D4");
            //环境温度4-6
            Stage4PLCAddress.Add("1D5");
            Stage4PLCAddress.Add("1D6");
            Stage4PLCAddress.Add("1D7");

            StagePlcAddressList.Add(Stage1PLCAddress);
            StagePlcAddressList.Add(Stage2PLCAddress);
            StagePlcAddressList.Add(Stage3PLCAddress);
            StagePlcAddressList.Add(Stage4PLCAddress);
            StagePlcAddressList.Add(Stage1PLCAddress);

        }

        /// <summary>
        /// 画1系界面方法
        /// </summary>
        /// <param name="titleNum"></param>
        /// <param name="pnlNum"></param>
        /// <param name="rowNum"></param>
        /// <param name="i"></param>
        private void pubFun(int titleNum, Panel pnlNum, int rowNum, int i)
        {
            labSense = new Label();//实例化

            if (Columns > PowerColumnNo || Columns == 1)
            {
                //是否换行，或者是否是第一行，是的话则初始化列号
                Columns = 1;
                //if (Columns == 1 && rowNum % 2 == 1)
                //{
                //判读是否是标题行,标题行的话生成labelTitle
                LocationY = Convert.ToInt32((pnlNum.Height - 50) / (serialMaxRowNo) / avgpanel * (rowNum - 1));
                Label labTitle = new Label();//实例化
                labTitle.Name = "labTitle" + i;//名称
                labTitle.Width = 70;//宽度
                                    //labTitle.Height = (pnlNum.Height - 50) / 2;//高度
                labTitle.Height = Convert.ToInt32((pnlNum.Height - 50) / (serialMaxRowNo) / avgpanel);
                labTitle.BorderStyle = BorderStyle.FixedSingle;//边框
                labTitle.BackColor = Color.FromArgb(7, 34, 72);//异常背景红色
                labTitle.ForeColor = Color.White;
                labTitle.TextAlign = ContentAlignment.MiddleCenter;//文本显示位置
                labTitle.Text = (5-rowNum) + "层";
                labTitle.Location = new Point(78, LocationY);//定位
                pnlNum.Controls.Add(labTitle);//信息绑定到控件中并显示
                labSense.Location = new Point(78, LocationY);//定位  
            }
            labSense.Name = "label" + (i*4+5-rowNum);//名称
            labSenseWidth = (pnlGuideOne.Width - 150) / columnNo;
            codeTitle = "1" + "-" + Columns + "-" + (5-rowNum);
            
            labSense.Location = new Point((Columns - 1) * labSenseWidth + 148, LocationY);//定位
                                                                                          
            labSense.Width = labSenseWidth;//宽度
            //labSense.Height = (pnlNum.Height - 50) / 2;//高度
            labSense.Height = Convert.ToInt32((pnlNum.Height - 50) / (serialMaxRowNo) / avgpanel);
            labSense.BorderStyle = BorderStyle.FixedSingle;//边框
            labSense.TextAlign = ContentAlignment.MiddleCenter;//文本显示位置
            labSense.ForeColor = Color.White;
            labSense.BackColor = Color.DarkGreen;//正常背景绿色

            labSense.Text = codeTitle;
            labSense.Font = new System.Drawing.Font("宋体", 9F);
            labSense.Click += new EventHandler(labSense_MouseDown);
            labSense.MouseHover += new EventHandler(labSense_MouseOver);
            //labSense.MouseLeave += new EventHandler(labSense_MouseLeave);
            labSense.Cursor = Cursors.Hand;
            pnlNum.Controls.Add(labSense);//信息绑定到控件中并显示
            //pnlNum.Font = new Font("微软雅黑", 13, pnlGuideThree.Font.Style);
            Columns++;
        }


        /// <summary>
        ///将异常信息显示到dgv上
        /// </summary>
        /// <param name="info"></param>
        /// <param name="isError"></param>
        public void displayInfo(string info, bool isError)
        {
            if (string.IsNullOrEmpty(info))
            {
                return;
            }
            try
            {
                if (this.dgvInfo.Rows.Count > 100)
                {
                    for (int i = this.dgvInfo.Rows.Count - 1; i > 10; i--)
                    {
                        if (this.dgvInfo.Rows.Count != 0)
                        {
                            this.Invoke(new Action(() =>
                            {
                                this.dgvInfo.Rows.RemoveAt(this.dgvInfo.Rows.Count - 2);
                            }));
                        }
                    }
                }

                this.Invoke(new Action(() =>
                {
                    this.dgvInfo.Rows.Insert(0, new object[]
                {
                    DateTime.Now.ToString("[yyyy/MM/dd HH:mm:ss fff]") + (isError ? "[ERROR]" : "[INFO]") + info
                });
                }));
                
                if (isError)
                {
                    this.Invoke(new Action(() =>
                    {
                        this.dgvInfo.Rows[0].DefaultCellStyle.BackColor = Color.Yellow;
                    }));
                    Log.AddLog(LogFile.SystemError,info);
                }
                else
                {
                }
            }
            catch (Exception ex)
            {
            }
        }


        /// <summary>
        /// 初始化温度数据Datatable
        /// </summary>
        public void InitTempDataDT()
        {
            TempCanData.TempDt.Columns.Add("chno", typeof(int));
            TempCanData.TempDt.Columns.Add("temp", typeof(string));
            for (int i = 0; i < 18; i++)
            {
                DataRow dr = TempCanData.TempDt.NewRow();
                dr["chno"] = (i + 1);
                dr["temp"] = "10";
                TempCanData.TempDt.Rows.Add(dr);
            }
        }

        /// <summary>
        /// 心跳检测
        /// </summary>
        public void CheckSenseOffLine()
        {

            while (!WantExit)
            {
                DateTime NowDt = DateTime.Now;
                for (int i = 1; i <= SenseCountNo; i++)
                {
                    DataRow[] AliveDr = TempCanData.AliveTraceDt.Select("SenseId=" + i);
                    if (AliveDr.Length > 0)
                    {
                        
                        try
                        {
                            DateTime TraceTime = Convert.ToDateTime(AliveDr[0]["ChangeTime"]);
                            string SenseLbName = "label" + i;
                            if (NowDt.Subtract(TraceTime).TotalSeconds > 10)
                            {

                                if (((Label)(this.Controls.Find(SenseLbName, true)[0])).BackColor == Color.DarkGreen)
                                {
                                    ((Label)(this.Controls.Find(SenseLbName, true)[0])).BackColor = Color.Red;
                                }
                            }
                            else
                            {
                                ((Label)(this.Controls.Find(SenseLbName, true)[0])).BackColor = Color.DarkGreen;
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.AddLog(LogFile.SystemError, "更改基板" + SenseCountNo + "的状态失败" + ex.Message);
                        }
                    }
                }

                /*更新PLC连接状态*/
                if (!PLCConnectStatus)
                {
                    displayInfo(ConnectFalseInfo,true);
                }
                Thread.Sleep(2000);
            }

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void pnlData_Paint(object sender, PaintEventArgs e)
        {

        }
        private void pnlData_MouseMove(object sender, MouseEventArgs e)
        {

        }

        private void pnlData_MouseDown_1(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);//*********************调用移动无窗体控件函数
        }

        private void notifyIcon1_MouseDoubleClick_1(object sender, MouseEventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void lblTitle_Click(object sender, EventArgs e)
        {
            
        }

        private void groupBoxMenu_Enter(object sender, EventArgs e)
        {

        }



        /// <summary>
        /// 初始化心跳追踪Datatable
        /// </summary>
        public void InitAliveTraceDT()
        {
            TempCanData.AliveTraceDt.Columns.Add("SenseId", typeof(int));
            DateTime Now = DateTime.Now;   
            TempCanData.AliveTraceDt.Columns.Add("ChangeTime", typeof(DateTime));
            for (int i = 0; i < SenseCountNo; i++)
            {
                DataRow dr = TempCanData.AliveTraceDt.NewRow();
                dr["SenseId"] = (i + 1);
                dr["ChangeTime"] = Now;
                TempCanData.AliveTraceDt.Rows.Add(dr);
            }
        }

        private void labSense_MouseOver(object sender, EventArgs e)
        {
            Label lb = (Label)sender;

            if (tip != null)
            {
                tip.Dispose();
            }
            tip = new ToolTip();
            tip.ShowAlways = true;
            //string str = lb.Name.Substring(lb.Name.LastIndexOf("-") + 1);
            //if (Convert.ToInt32(str) <= 8)
            //{
                tip.SetToolTip(lb, lb.Name);
            //}
        }
        private void labSense_MouseDown(object sender, EventArgs e)
        {
            Label lb = (Label)sender;
            string lblname = lb.Name;
            ShowSenseCodeId = Convert.ToInt32(lblname.Substring(5));
            FormDataShow show = new FormDataShow();
            show.ShowDialog();
        }
    }

}
